<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * 圈圈 Service
 *
 */
class SDOServiceQuanquan extends ServiceBase
{
    # 正式环境Quanquan WSDL URL
    const QQ_WSDL = 'QQ_WSDL';
    # 测试环境Quanquan WSDL URL
    const PRE_QQ_WSDL = 'PRE_QQ_WSDL';
    # 接口调用密码
    const WS_PASSWORD = '098#@!';
    /// <param name="appId">游戏id</param>
    /// <param name="appName">游戏名称</param>
    /// <param name="principal">负责人</param>
    /// <param name="cellPhone">手机</param>
    /// <param name="telephone">座机</param>
    /// <param name="email">电子邮件</param>
    /// <param name="manager">大客户经理姓名</param>
    /// <param name="developeEngine">开发引擎</param>
    /// <param name="graphicMode">图形模式</param>
    /// <param name="areaServerStruct">区服结构 /param>
    /// <param name="loginMode">登陆模式</param>
    /// <param name="areaGroupMode">游戏选区、组模式</param>
    /// <param name="fullScreenMode">全屏模式</param>
    /// <param name="areaGroupIdValid">初始传入区组ID是否有效</param>
    /// <param name="pigment">色深</param>
    /// <param name="websitePV">网站每日pv</param>
    /// <param name="websiteType">网站类型</param>
    /// <param name="password">核对密码，防止外部非法调用</param>
    /// <returns>成功返回true，失败会抛出异常</returns>
    /**
     * 添加SP信息
     *
     * @param string $message
     * @param array $params  
     * @param string $env
     * @return Boolean
     */
    public static function AddSpInfo (&$message, $params, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $param_keys = array(
            'appId', 
            'appName', 
            'principal', 
            'cellPhone', 
            'telephone', 
            'email', 
            'manager', 
            'developeEngine', 
            'graphicMode', 
            'areaServerStruct', 
            'loginMode', 
            'areaGroupMode', 
            'fullScreenMode', 
            'areaGroupIdValid', 
            'pigment'
        );
        $validate = self::_validateParam($params, $param_keys, $message);
        if ($validate === FALSE) {
            return FALSE;
        }
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('AddSpInfo', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $json = json_decode(trim($tmp[1]));
                if ($json->ErrorCode == 10101) {
                    return true;
                }
            }
            if ($json && $json->message) {
                $message = $json->Message;
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        self::log(
        "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " . intval($result), 
        __CLASS__, true);
        return TRUE;
    }
    /**
     * 获取SP信息
     *
     * @param string $message
     * @param int $appId
     * @param string $env
     * @return obj|false
     */
    public static function GetSpInfo (&$message, $appId, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('GetSpInfo', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            return FALSE;
        }
        return $result;
    }
    /**
     * 更新SP信息
     *
     * @param string $message
     * @param array $params  
     * @param string $env
     * @return Boolean
     */
    public static function EditSpInfo (&$message, $params, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $param_keys = array(
            'appId', 
            'appName', 
            'principal', 
            'cellPhone', 
            'telephone', 
            'email', 
            'manager', 
            'developeEngine', 
            'graphicMode', 
            'areaServerStruct', 
            'loginMode', 
            'areaGroupMode', 
            'fullScreenMode', 
            'areaGroupIdValid', 
            'pigment'
        );
        $validate = self::_validateParam($params, $param_keys, $message);
        if ($validate === FALSE) {
            return FALSE;
        }
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('EditSpInfo', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $message = $tmp[1];
                $json = json_decode(trim($tmp[1]));
                if ($json && $json->message) {
                    $message = $json->Message;
                }
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        self::log(
        "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: ". intval($result) , 
        __CLASS__, true);
        return TRUE;
    }
    /**
     * 开通IGW
     *
     * @param string $message
     * @param int $appId
     * @param string $color 颜色#FFFFFF
     * @param string $skinType 皮肤类型
     * @param string $env
     * @return boolean
     */
    public static function IgwApply (&$message, $appId, $color, $skinType, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['color'] = $color;
        $params['skinType'] = $skinType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IgwApply', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $message = $tmp[1];
                $json = json_decode(trim($tmp[1]));
                if ($json && $json->message) {
                    $message = $json->Message;
                }
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        if ($result->IgwApplyResult) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             json_encode($result), __CLASS__, true);
            return TRUE;
        }
        self::log(
        "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " . $message, 
        __CLASS__);
        return FALSE;
    }
    /**
     * 开通 IBW
     *
     * @param string $message
     * @param int $appId
     * @param string $domain
     * @param string $env
     * @return boolean
     */
    public static function IbwApply (&$message, $appId, $domain, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['domain'] = $domain;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IbwApply', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $message = $tmp[1];
                $json = json_decode(trim($tmp[1]));
                if ($json && $json->message) {
                    $message = $json->Message;
                }
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        self::log(
        "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " . intval(
        $result), __CLASS__, true);
        return TRUE;
    }
    /**
     * 获取IGW配置信息
     * 第一个数组是皮肤列表
     * 第2个是igw颜色列表
     * 第3个是ibw颜色列表
     * @param string $message
     * @param string $env
     * @return Obj|Boolean
     */
    public static function GetConfig (&$message, $env = 'pre')
    {        
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IGWConfig', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            self::log(
        	"method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}", __CLASS__);
            return FALSE;
        }
        return $result->IGWConfigResult->ArrayOfRegModel;
    }
    /**
     * 获取Widget配置信息
     *
     * @param string $message
     * @param int $appId
     * @param string $widgetType widget类型，0:igw,1:Ibw
     * @param string $env
     * @return Obj|Boolean $widgetTable IGW时 object(
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号,
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * Description => 描述,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：启用   
     * )
     * IBW时 object(
     * Idx => 主键ID,
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * Description => 描述,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：启用 ,
     * Position => Widget所在位置(0 工具集, 1 用户工具, 2 固定栏工具,3 SP专属工具, 
     * 4 侧边栏工具, 5 工具条推荐工具, 6 工具条高亮工具,7 最小化横条工具)
     * )
     */
    public static function GetWidgetList (&$message, $appId, $widgetType, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['type'] = $widgetType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('GetWidgetList', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            return FALSE;
        }
        return $result->GetWidgetListResult->AppWidgetStatus;
    }
    /**
     * 提交widget列表的使用信息
     *
     * @param string $message
     * @param int $appId
     * @param array $widgetTable IGW时 array(
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号,
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：禁用   
     * )
     * IBW时 array(
     * Idx => 主键ID,
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * SPStatus => 当前商户该Widget使用情况 0：使用,-1：启用 ,
     * Position => Widget所在位置(0 工具集, 1 用户工具, 2 固定栏工具,3 SP专属工具, 
     * 4 侧边栏工具, 5 工具条推荐工具, 6 工具条高亮工具,7 最小化横条工具)
     * )
     * @param int $widgetType widget类型，0:igw,1:Ibw
     * @param string $env
     * @param boolean $auto 当$env为正式环境，且$auto为true时，自动将$widgetTable联调环境内容，转为正式环境的$widgetTable
     * @return Boolean
     */
    public static function UpdateWidgetList (&$message, $appId, $widgetTable, $widgetType, $env = 'pre', $auto = false)
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        if ($auto && $env != 'pre') {
            self::parseWidgetTable($widgetTable, $appId, $widgetType);
        }
        if (! $widgetTable && ! $widgetType) {
            return true;
        }
        $params['widgetTable'] = $widgetTable;
        $params['type'] = $widgetType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('UpdateWidgetList', $params, $wsdl, $options, $message);
        if ($result === FALSE || $result->UpdateWidgetListResult == false) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $message = $tmp[1];
                $json = json_decode(trim($tmp[1]));
                if ($json && $json->message) {
                    $message = $json->Message;
                }
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        self::log(
        "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " . json_encode(
        $result), __CLASS__, true);
        return TRUE;
    }
    /**
     * 联调环境widget列表转义为正式环境的信息
     * @param array $widgetTable
     * @param int $appId
     * @param int $widgetType
     * 
     * @return void
     */
    private static function parseWidgetTable (&$widgetTable, $appId, $widgetType)
    {
        $message = '';
        $wdList = array();
        $preTrans = array();
        $tmp = self::GetWidgetList($message, $appId, $widgetType, "online");
        foreach ($tmp as $obj) {
            $wdList[$obj->NameSpace] = (array) $obj;
        }
        $tmp = self::GetWidgetList($message, $appId, $widgetType, "pre");
        foreach ($tmp as $obj) {
            $preTrans[$obj->WidgetGUID . "-" . $obj->Version] = $obj->NameSpace;
        }
        unset($tmp);
        foreach ($widgetTable as $k => $row) {
            $index = $row['WidgetGUID'] . "-" . $row['Version'];
            if (isset($preTrans[$index]) && $tmp = $wdList[$preTrans[$index]]) {
                $widgetTable[$k]['WidgetGUID'] = $tmp['WidgetGUID'];
                $widgetTable[$k]['Version'] = $tmp['Version'];
            }
        }
    }
    /**
     * 配置IBW皮肤
     *
     * @param string $message
     * @param int $barMode
     * @param string $color
     * @param boolean $useServerConfig
     * @param int $appId
     * @param string $env
     * @return Boolean
     */
    public static function UpdateIBWSkin (&$message, $barMode, $color, $useServerConfig, $appId, $env = 'pre')
    {
        $wsdl = $env === 'pre' ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['barMode'] = $barMode;
        $params['color'] = $color;
        $params['useServerConfig'] = $useServerConfig;
        $params['appId'] = $appId;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('UpdateIBWSkin', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            $tmp = explode("--->", $message);
            if ($tmp[1]) {
                $message = $tmp[1];
                $json = json_decode(trim($tmp[1]));
                if ($json && $json->message) {
                    $message = $json->Message;
                }
            }
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\treturn: " .
             $message, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        return TRUE;
    }
    /**
     * 自检函数
     */
    public static function selfTest ()
    {
        $arr = array(
            sdk_fetch_config(SDK_ENV . self::QQ_WSDL), 
            sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL)
        );
        return self::curlTest($arr);
    }
}
